package cn.iocoder.yudao.module.reclaim.service.productcheckdeductitem;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.reclaim.controller.admin.productcheckdeductitem.vo.ReclaimCheckDeductItemPageReqVO;
import cn.iocoder.yudao.module.reclaim.controller.admin.productcheckdeductitem.vo.ReclaimCheckDeductItemSaveReqVO;
import cn.iocoder.yudao.module.reclaim.dal.dataobject.productcheckres.ReclaimCheckDeductItemDO;
import cn.iocoder.yudao.module.reclaim.dal.mysql.productcheckres.ReclaimCheckDeductItemMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.reclaim.enums.ErrorCodeConstants.PRODUCT_CHECK_DEDUCT_ITEM_NOT_EXISTS;

/**
 * 回收核验扣除项 Service 实现类
 *
 * @author FFFF
 */
@Service
@Validated
public class ReclaimCheckDeductItemServiceImpl implements ReclaimCheckDeductItemService {

    @Resource
    private ReclaimCheckDeductItemMapper productCheckDeductItemMapper;

    @Override
    public Long createProductCheckDeductItem(ReclaimCheckDeductItemSaveReqVO createReqVO) {
        // 插入
        ReclaimCheckDeductItemDO productCheckDeductItem = BeanUtils.toBean(createReqVO, ReclaimCheckDeductItemDO.class);
        productCheckDeductItemMapper.insert(productCheckDeductItem);
        // 返回
        return productCheckDeductItem.getId();
    }

    @Override
    public void updateProductCheckDeductItem(ReclaimCheckDeductItemSaveReqVO updateReqVO) {
        // 校验存在
        validateProductCheckDeductItemExists(updateReqVO.getId());
        // 更新
        ReclaimCheckDeductItemDO updateObj = BeanUtils.toBean(updateReqVO, ReclaimCheckDeductItemDO.class);
        productCheckDeductItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductCheckDeductItem(Long id) {
        // 校验存在
        validateProductCheckDeductItemExists(id);
        // 删除
        productCheckDeductItemMapper.deleteById(id);
    }

    private void validateProductCheckDeductItemExists(Long id) {
        if (productCheckDeductItemMapper.selectById(id) == null) {
            throw exception(PRODUCT_CHECK_DEDUCT_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public ReclaimCheckDeductItemDO getProductCheckDeductItem(Long id) {
        return productCheckDeductItemMapper.selectById(id);
    }

    @Override
    public PageResult<ReclaimCheckDeductItemDO> getProductCheckDeductItemPage(ReclaimCheckDeductItemPageReqVO pageReqVO) {
        return productCheckDeductItemMapper.selectPage(pageReqVO);
    }

}